package com.chenliang.consu.listener;


import com.rabbitmq.client.Channel;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @program: springboot_rabbitmq_all
 * @description:
 * @author: 陈亮
 * @create: 2022-05-10 15:41
 */
@Component
public class ConsumerListener {


    @RabbitListener(queues = {"log_q"})
    public void receive01(String message, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws IOException, InterruptedException {


//        try {
//            System.out.println("准备处理的消息是:" + message);
//            int a=1/0;
//            System.out.println("处理业务逻辑,准备手动签收消息");
//            channel.basicAck(deliveryTag, true);
//            System.out.println("消息处理成功");
//        } catch (Exception e) {
//            System.out.println("消息处理失败");
//            channel.basicNack(deliveryTag, true, true);
//        }

        TimeUnit.SECONDS.sleep(4);
        System.out.println(message);
        channel.basicAck(deliveryTag, true);
    }


    /**
     * 消费端ack:
     */

    /**
     * 消费端限流 其实就是消费端每次从队列拉取数据的数量
     * 1:消费端开启手动ack
     * 2: 配置文件rabbitMq配置prefetch属性,prefetch的value表示消费者每次从队列拉取消息的数量
     */

}
